package com.bo.ivolatile;

/**
 * @Author: gpb
 * @Date: 2024/7/30 07:33
 * @Description:
 */
public class VisibilityCASE {
    static boolean flag = true;

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            while (flag) {

            }
            System.out.println(Thread.currentThread().getName() + "线程结束");
        });
        thread.start();
        Thread.sleep(100);
        flag = false;
        System.out.println();
        System.out.println(Thread.currentThread().getName() + "线程修改 flag 为 false");
        // 此时你会发现 thread线程还是没有结束
        // 说明可见性问题存在，thread拿的自己缓存数据flag还是true
    }
}
